package testcases;

import static org.junit.Assert.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

import javax.swing.JCheckBox;
import javax.swing.JFormattedTextField;
import javax.swing.JTextField;

import juf.FieldProperty;
import juf.MethodProperty;
import juf.adapter.Adapter;
import juf.adapter.AdapterFactory;
import juf.adapter.JCheckBoxAdapter;
import juf.adapter.JDatePickerAdapter;
import juf.adapter.JTextFieldAdapter;

import org.junit.Test;

public class ValidationTest {
	
	
	@Test
	public void testValidation_DatePicker(){
		DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
		try {
			Date d = dateFormat.parse("Jan 12, 1952");
			Student fixture = new Student(123,"Somename","", d, true, 0);
			
			MethodProperty mp = new MethodProperty(fixture.getClass().getMethod("getDateofbirth"), fixture.getClass().getMethod("setDateofbirth", Date.class), fixture);
			Adapter a = AdapterFactory.createAdapter(mp);
			assertEquals("Jan 12, 1952", ((JTextField)(a.getDataComponent())).getText());
			((JTextField)(a.getDataComponent())).setText("Jan 23, 1999"); //change the date of birth to something else
			a.save();
			assertEquals(dateFormat.parse("Jan 23, 1999"), fixture.getDateofbirth()); //should have updated the object
			assertEquals(true, a.validateField()); //should have validated
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			fail("unhandled exception" + e.toString());
			//ParseException p = null;
			//assertTrue(e.equals(p));
		}

	}
	
	@Test
	public void testValidation_DatePicker_Exception(){
		DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
		Adapter a = null;
		Student fixture = null;
		Date d = null;
		try {
			d = dateFormat.parse("Jan 12, 1952");
			fixture = new Student(123,"Somename","", d, true, 0);
			
			MethodProperty mp = new MethodProperty(fixture.getClass().getMethod("getDateofbirth"), fixture.getClass().getMethod("setDateofbirth", Date.class), fixture);
			a = AdapterFactory.createAdapter(mp);
			assertEquals("Jan 12, 1952", ((JTextField)(a.getDataComponent())).getText());
			((JTextField)(a.getDataComponent())).setText("Jan 23qw, 1999"); //set to garbled date, should not parse
			a.save();
			fail("uncaught exception"); //exception was not handled
			
		} catch (ParseException e) { //this is expected 
			assertEquals(false, a.validateField()); //did not validate
			assertEquals(d, fixture.getDateofbirth()); //value should not change
	
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	
	@Test
	public void testValidation_TextField(){
		Student fixture = new Student(123,"Hello World","", new Date(), true, 0);
		try {
			FieldProperty fp = new FieldProperty(fixture.getClass().getField("studentId"),fixture);
			Adapter a = AdapterFactory.createAdapter(fp);
			((JFormattedTextField) a.getDataComponent()).setText("42");//setting an int value
			a.save(); //then calling save on the adapter
			assertEquals(true, a.validateField()); //means validation was successful
			
			((JFormattedTextField) a.getDataComponent()).setText("text");//but setting a string where int is expected
			a.save(); //then calling save on the adapter
			assertNotSame("text", fixture.studentId); //will mean that the string did not get set
			assertEquals(42, fixture.studentId);
			assertFalse(a.validateField());//and that the validation was unsuccessful
		
			a.refresh(); //calling refresh
			assertEquals(true, a.validateField()); //means the data went back to previous succe
			
			
		} 
		catch (Exception e){
			fail("unhandled exception" + e.toString());
		}
		
	}

}
